package com.gensdai.leliang.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gensdai.leliang.R;
import com.gensdai.leliang.adapter.Grant_Bei_Deal_adapter;
import com.gensdai.leliang.base.BaseActivityNoAbs;
import com.gensdai.leliang.dialog.Upload;
import com.gensdai.leliang.entity.User;
import com.gensdai.leliang.json_base.json_base;
import com.gensdai.leliang.remoteInterface.ApiService;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/6/28.
 */

public class Grant_Bei_Deal extends BaseActivityNoAbs implements View.OnClickListener {

    @BindView(R.id.ui_title)
    TextView uiTitle;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.get_czb)
    TextView getCzb;  // 赚取按钮
    @BindView(R.id.no_czb)
    LinearLayout noCzb; // 没有数据父布局
    @BindView(R.id.get_czb_list)
    ListView getCzbList;
    @BindView(R.id.has_czb)
    LinearLayout hasCzb; // listview父布局

    Grant_Bei_Deal_adapter adapter;
    List<User> mlistdata = null;
    User user;
    SharedPreferences share;
    Upload up;

    private final int ENTER = 1;// 登录标识
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case ENTER:
                    up.dismiss();
                    try {
                        if (new json_base().GOOD((String) msg.obj)) {
                            mlistdata = (List<User>) new json_base().JsonGrantBei(new JSONObject((String) msg.obj));
                            setmListView();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    new JSONObject((String) msg.obj)
                                            .getString("message"),
                                    Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    up.dismiss();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grant_bei_deal);
        ButterKnife.bind(this);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        uiTitle.setText("成长贝发放记录");
        share = getSharedPreferences("User", MODE_PRIVATE);
        up = new Upload(this);
        back.setOnClickListener(this);
        getCzb.setOnClickListener(this);
        FindGrowShell();
    }

    /**
     * 设置list
     */
    private void setmListView() {

        if (mlistdata != null && mlistdata.size() > 0) {
            adapter = new Grant_Bei_Deal_adapter(this, mlistdata);
            getCzbList.setAdapter(adapter);
            hasCzb.setVisibility(View.VISIBLE);
            noCzb.setVisibility(View.GONE);
        } else {
            getCzbList.setVisibility(View.GONE);
            noCzb.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.get_czb:

                this.finish();
                break;
            default:
                break;
        }
    }


    /**
     * 调用查看成长贝发放接口
     */
    private void FindGrowShell() {
        //获取edit的值后请求
        Map<String, String> loginParams = new HashMap<>();
        loginParams.put("userNo", share.getString("userno", ""));   //用户编号
        toStr(loginParams);
    }

    //返回字符串类型
    private void toStr(Map<String, String> loginParams) {
        up.show();
        ApiService.getInstance(this).FindGrowShell(loginParams)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }

                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();

                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        //处理得到的数据
                        Log.e("ActionEnter", s);
                        Message msg = new Message();
                        msg.what = ENTER;
                        msg.obj = s;
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
//                        Log.e("onError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理
                        Log.d("onComplete", "onComplete");
                    }
                });
    }
}
